package com.lanlian.commonlib.upyun;

import android.content.Context;

import com.lanlian.utillib.CommonUtils;
import com.upyun.library.common.Params;
import com.upyun.library.common.UploadEngine;
import com.upyun.library.listener.UpCompleteListener;
import com.upyun.library.listener.UpProgressListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class UploadPic {

    private final static String BUCKET_NAME = UpYunConstants.getUpYunBucketName();
    private final static String OPERATOR_NAME = UpYunConstants.getUpYunOperateName();
    private final static String OPERATOR_PWD = UpYunConstants.getUpYunOperatePassword();

    public static String savePath = "/FuturesManage/{year}{mon}/{filename}{.suffix}";

    /**
     * @param context
     * @param filePath
     * @param callback
     */
    public static void uploadFileWithCompress(Context context, String filePath, IUploadFileCallback callback) {
        File imageFile = new File(filePath);
        compressAndUpload(context, imageFile, callback);
    }

    public static void compressAndUpload(Context context, File imageFile, final IUploadFileCallback callback) {
        Luban.with(context).load(imageFile).setCompressListener(new OnCompressListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(File file) {
                if (file != null) {
                    uploadFile(file.getAbsolutePath(), callback);
                } else {
                    MUploadFile mUploadFile = new MUploadFile();
                    mUploadFile.setResult(false);
                    callback.onData(mUploadFile);
                }
            }

            @Override
            public void onError(Throwable e) {
                MUploadFile mUploadFile = new MUploadFile();
                mUploadFile.setResult(false);
                callback.onData(mUploadFile);
            }
        }).launch();
    }

    public static void uploadFile(String filePath, final IUploadFileCallback callback) {
        final MUploadFile mUploadFile = new MUploadFile();
        mUploadFile.setFile(filePath);
        try {
            final Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put(Params.BUCKET, BUCKET_NAME);
            paramsMap.put(Params.SAVE_KEY, savePath);
            paramsMap.put(Params.X_GMKERL_ROTATE, "auto");//设置旋转
            paramsMap.put(Params.RETURN_URL, "httpbin.org/post");

            UpProgressListener progressListener = new UpProgressListener() {
                @Override
                public void onRequestProgress(final long bytesWrite, final long contentLength) {
                    callback.onProgress((int) ((100 * bytesWrite) / contentLength));
                }
            };

            UpCompleteListener completeListener = new UpCompleteListener() {
                @Override
                public void onComplete(boolean isSuccess, String result) {
                    try {
                        if (isSuccess) {
                            JSONObject jsonObject = new JSONObject(result);
                            mUploadFile.setUrl(jsonObject.getString("url"));
                            mUploadFile.setResult(true);
                            mUploadFile.setWidth(jsonObject.getInt("image-width"));
                            mUploadFile.setHeight(jsonObject.getInt("image-height"));
                            callback.onData(mUploadFile);
                        } else {
                            mUploadFile.setResult(false);
                            callback.onData(mUploadFile);
                        }
                    } catch (JSONException e) {
                        mUploadFile.setResult(false);
                        callback.onData(mUploadFile);
                        e.printStackTrace();
                    }
                }
            };
            UploadEngine.getInstance().formUpload(new File(filePath), paramsMap, OPERATOR_NAME,
                    CommonUtils.md5(OPERATOR_PWD), completeListener, progressListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
